package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import model.WriteInformationInToDB;
import java.io.*;
import java.awt.*;
import javax.swing.*;

public class BrowseList implements ActionListener {
	JFrame frame = new JFrame("文件选择");
	JTabbedPane tabPane = new JTabbedPane();// 选项卡布局
	Container con = new Container();// 布局1
	Container con1 = new Container();// 布局2
	JLabel label2 = new JLabel("请选择文件(txt格式)：");
	JTextField text2 = new JTextField();
	JTextArea text4 = new JTextArea();
	JScrollPane spane = new JScrollPane();
	JButton button2 = new JButton("...");
	JButton button3 = new JButton("OK");
	JFileChooser jfc = new JFileChooser();// 文件选择器

	BrowseList() {
		jfc.setCurrentDirectory(new File("d:\\"));// 文件选择器的初始目录定为d盘
		// 下面两行是取得屏幕的高度和宽度
		double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		frame.setLocation(new Point((int) (lx / 2) - 150, (int) (ly / 2) - 150));// 设定窗口出现位置
		frame.setSize(500, 500);// 设定窗口大小
		frame.setContentPane(tabPane);// 设置布局
		// 下面设定标签等的出现位置和高宽
		label2.setBounds(10, 30, 200, 20);
		text2.setBounds(120, 30, 120, 20);
		text4.setBounds(30, 100, 300, 200);
		button2.setBounds(280, 30, 50, 20);
		button3.setBounds(100, 70, 51, 20);
		button2.addActionListener(this);// 添加事件处理
		button3.addActionListener(this);
		spane.setBounds(30, 100, 300, 200);
		con.add(spane);
		con.add(label2);
		con.add(text2);
		con.add(button2);
		con.add(button3);
		con.add(jfc);
		spane.setViewportView(text4);
		tabPane.add("目录/文件选择", con);// 添加布局1
		frame.setVisible(true);// 窗口可见
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);// 只关闭当前窗口

	}

	public void readFile() {
		String filename2 = text2.getText();
		FileReader fr = null;
		BufferedReader br = null;
		try {
			fr = new FileReader(filename2);
			br = new BufferedReader(fr);
			String str;
			while ((str = br.readLine()) != null) {
				text4.append(str + "\n");
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {// 事件处理
		if (e.getSource().equals(button2)) {// 判断触发方法的按钮是哪个
			jfc.setFileSelectionMode(0);// 设定只能选择到文件
			int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
			if (state == 1) {// 选择文件state为0
				return;// 撤销则返回
			} else {
				File f = jfc.getSelectedFile();// f为选择到的文件
				text2.setText(f.getAbsolutePath());
			}
		}
		if (e.getSource().equals(button3)) {// 确定好选择的TXT文件了
			try {
				String tname = WriteInformationInToDB.writeToDB(text2.getText());
				readFile();
				YFileChooser y = new YFileChooser();
				y.textName.setText(tname);
				y.frame.setVisible(true);
				// frame.setVisible(false);
			} catch (IOException e1) {

				e1.printStackTrace();
			}
		}

	}
}